using System;
using System.IO;
using System.Runtime.InteropServices;
using Microsoft.VisualStudio.Package;
using Microsoft.VisualStudio;
using MH.WsdlWorks.ProjectModel;

namespace MH.WsdlWorks
{
    [CLSCompliant(false)]
    [ComVisible(true)]
    [Guid("F0F80C5C-BC50-4072-BFFA-99E27B067CA3")]
    public class OperationNode : NodeBase
    {
        // type guid of an Operation Node
        Guid typeGuid = new Guid("DB9D2B18-57AD-40bc-A094-026969436A95");
        readonly Operation operation;

        public Operation Operation
        {
            get { return operation; }
        }

        public OperationNode(SimpleProjectNode root, Operation operation) 
            : base(root, operation.GetPath())
        {
            this.operation = operation;

            foreach (Request request in operation.Requests)
            {
                AddRequestNode(request);
            }
        }

        protected override SimpleNodeProperties CreatePropertiesObject()
        {
            return new OperationNodeProperties(this);
        }

        public override object GetIconHandle(bool open)
        {
            // TODO: create and return an operation node icon
            return this.ProjectMgr.ImageHandler.GetIconHandle((int)ProjectNode.ImageName.Reference);
        }

        public override Guid ItemTypeGuid
        {
            get { return this.typeGuid; }
        }

        public override int MenuCommandId
        {
            get
            {
                return MenuIdList.menuIdOperationNode;
            }
        }

        public override int SortPriority
        {
            get
            {
                return DefaultSortOrderNode.FolderNode;
            }
        }

        protected override int QueryStatusOnNode(Guid guidCmdGroup, uint cmd, IntPtr pCmdText, ref QueryStatusResult result)
        {
            if (guidCmdGroup == GuidList.guidTestMenuPackageCmdSet)
            {
                if (cmd == PackageCommandIdList.cmdidAddRequest)
                {
                    result |= QueryStatusResult.SUPPORTED | QueryStatusResult.ENABLED;
                    return VSConstants.S_OK;
                }
            }
            return base.QueryStatusOnNode(guidCmdGroup, cmd, pCmdText, ref result);
        }

        protected override int ExecCommandOnNode(Guid guidCmdGroup, uint cmd, uint nCmdexecopt, IntPtr pvaIn, IntPtr pvaOut)
        {
            if (guidCmdGroup == GuidList.guidTestMenuPackageCmdSet)
            {
                if (cmd == PackageCommandIdList.cmdidAddRequest)
                {
                    AddRequest();
                    return VSConstants.S_OK;
                }
            }
            return base.ExecCommandOnNode(guidCmdGroup, cmd, nCmdexecopt, pvaIn, pvaOut);
        }

        private void AddRequest()
        {
            Request request = operation.AddRequest();
            AddRequestNode(request);
        }

        public void AddRequestNode(Request request)
        {
            RequestNode requestNode = new RequestNode(this.ProjectMgr, request);
            this.AddChild(requestNode);
        }
    }
}
